package com.psct.social;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.psct.Constant;
import com.psct.bean.UserAccount;

public class UserAccountManager {
	private static UserAccountManager usm;
	private UserAccount user;

	private UserAccountManager() {

	}

	public static UserAccountManager getInstance() {
		if (usm == null) {
			usm = new UserAccountManager();
		}
		return usm;
	}

	public UserAccount getUser(Context context) {
		if(user==null) {
			user=this.getUserFromLocal(context);
		}
		return user;
	}

	public void setUser(UserAccount user) {
		this.user = user;
	}

	/**
	 * 保存帐号信息
	 * @param context
	 */
	public void saveAccount(Context context) {
		if (user != null) {
			this.getSP(context).edit().putString(Constant.SP_ACCOUNT, user.userName)
					.putString(Constant.SP_PWD, user.pwd).commit();
		}
	}

	private UserAccount getUserFromLocal(Context context) {
		
		String account = this.getSP(context).getString(Constant.SP_ACCOUNT, "");
		String pwd = this.getSP(context).getString(Constant.SP_PWD, "");
		if (!TextUtils.isEmpty(account)&&!TextUtils.isEmpty(pwd)) {
			UserAccount user = new UserAccount () ;
			user.userName=account;
			user.pwd=pwd;
			return user;
		}
		return null;
	}
	/**
	 * 取得用来读写的编辑器
	 * @param context
	 * @return
	 */
	private SharedPreferences getSP(Context context) {
		return context.getSharedPreferences("account_keep", Context.MODE_PRIVATE);
	}
}
